<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客列表</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/myblog_list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/jscommon.js"></script>
    <style>
        .blog-pagnation-wrapper{
            height: 40px;
            margin: 16px 0;
            text-align: center;
        }
    </style>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img/小灰灰.jpeg" alt="">
        <span class="title">我的博客系统</span>
        <span class="myspacer"></span>
        <div class="row">
            <input type="text" id="size" placeholder="在这里设置博客数/页(3-15)">
            <a href="javascript:setSize()">设置</a>
        </div>
        <!-- 用来占据中间位置 -->
        <span class="myspacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="myblog_draft_list.html">我的草稿</a>
        <a href="blog_add.html">写博客</a>
        <a href="javascript:logout()">注销</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="container">
        <!-- 左侧个人信息 -->
        <div class="container-left">
            <div class="card">
                <img src="img/智羊羊.jpeg" class="avtar" alt="">
                <h3 id="username"></h3>
                <a href="http:www.github.com">github 地址</a>
                <div class="counter">
                    <span>文章</span>
                    <span>分类</span>
                </div>
                <div class="counter">
                    <span id="artTotal"></span>
                    <span>1</span>
                </div>
            </div>
        </div>
        <!-- 右侧内容详情 -->
        <div id = "artDiv" class="container-right">
            <h1 style="text-align: center;">我的博客列表页</h1>
            <div id="articleList" style="overflow: auto">
                <!-- 每一篇博客包含标题, 摘要, 时间 -->

            </div>
            
            <hr>
            <div class="blog-pagnation-wrapper">
                <button class="blog-pagnation-item" onclick="goFirstPage()">首页</button> 
                <button class="blog-pagnation-item" onclick="goBeforePage()">上一页</button>  
                <button class="blog-pagnation-item" onclick="goNextPage()">下一页</button>
                <button class="blog-pagnation-item" onclick="goLastPage()">末页</button>
            </div>
        </div>
    </div>
    <script>
        function showInfo() {
            jQuery.ajax({
                url: "/user/showinfo",
                type: "GET",
                data: {},
                success: function(result) {
                    if(result != null && result.code == 200) {
                        jQuery("#username").text(result.data.username);
                        jQuery("#artTotal").text(result.data.artCount);
                    }else {
                        alert("用户信息加载失败！请刷新重试");
                    }
                }
            });
        }
        showInfo();

        // 分页功能
        // 1. 获取当前页码
        var page = (getUrlValue("page")=="" ? 1 : getUrlValue("page"));
        // 2. 获取当前页面最大博客数，由于我们还没给用户设置选项，因此暂时默认设置
        // var size = getUrlValue("size");
        var size = (getUrlValue("size")=="" ? 5 : getUrlValue("size"));
        // 3. 定义全局页数，从 getArtList函数中获取
        var pageCount = 0;
        function getMyArtList() {
            jQuery.ajax({
                url: "/art/mylist",
                type: "POST" ,
                data: {"page": page, "size": size},
                success: function(result) {
                    if(result != null && result.code == 200) {
                        // 判断用户是否发表过文章
                        if(result.data.list.length > 0) {
                            pageCount = result.data.pageCount;
                            // 发表了文章
                            var artListDiv = "";
                            for(var i = 0; i < result.data.list.length; i++) {
                                var artItem = result.data.list[i];
                                // 对内容进行拼接
                                artListDiv += '<div class="blog">';
                                artListDiv += '<div class="title">' + artItem.title + '</div>';
                                artListDiv += '<div class="date">发布时间：' + artItem.createtime + "&nbsp&nbsp&nbsp";
                                artListDiv += '最后一次修改：' + artItem.updatetime + '</div>';
                                artListDiv += '<div class="desc">';
                                artListDiv += artItem.content;
                                artListDiv += '</div>';
                                artListDiv += '<a href="blog_content.html?id=' + 
                                    artItem.id + '" class="detail">查看全文 &gt;&gt;</a>&nbsp;&nbsp;&nbsp;';
                                artListDiv += '<a href="blog_edit.html?id=' + 
                                    artItem.id + '" class="detail">修改文章 &gt;&gt;</a>&nbsp;&nbsp;&nbsp;';
                                artListDiv += '<a href="javascript: myDel(' + artItem.id + ');" class="detail">删除文章 &gt;&gt;</a>&nbsp;&nbsp;&nbsp;';
                                artListDiv += '</div>';
                            }
                            jQuery("#articleList").html(artListDiv);
                        }else {
                            // 未发表文章
                            jQuery("#articleList").html("<h3>暂无文章<h3>");
                        }
                    }else {
                        alert("查询文章出错，请刷新重试！");
                    }
                }
            });
        }
        getMyArtList();

        // 博客分页跳转
        // 转到首页
        function goFirstPage() {
            if(page <= 1) {
                alert("当前已经在首页！");
                return;
            }
            location.href = "myblog_list.html?page=1" + "size=" + size;
        }

        // 转到上一页
        function goBeforePage() {
            if(page <= 1) {
                alert("当前已经在首页！");
                return;
            }
            page = parseInt(page) - 1;
            location.href = "myblog_list.html?page=" + page + "&size=" + size;
        }

        // 转到下一页
        function goNextPage() {
            if(page >= pageCount) {
                alert("已经是最后一页！");
                return;
            }
            page = parseInt(page) + 1;
            location.href = "myblog_list.html?page=" + page + "&size=" + size;
        }

        // 转到最后一页
        function goLastPage() {
            if(page >= pageCount) {
                alert("已经是最后一页！");
                return;
            }
            location.href = "myblog_list.html?page=" + pageCount + "&size=" + size;
        }


        function myDel(id) {
            // 先确认用户是否真的要删除
            if(confirm("确认删除？")) {
                // 删除文章请求
                jQuery.ajax({
                    url: "/art/del",
                    type: "POST",
                    data: {"id": id},
                    success: function(result) {
                        if(result != null && result.code == 200 && result.data == 1) {
                            alert("文章删除成功！");
                            // 刷新当前页面
                            location.href = location.href;
                        }else {
                            alert("文章删除失败，请重试！")
                        }
                    }
                });
            }
        } 

        // 设置size
        function setSize() {
            var inputSize = jQuery("#size");
            if(inputSize.val() == "") {
                alert("请先输入值！");
                inputSize.focus();
                return;
            }
            if(inputSize.val() < 3) {
                size = 3;
            }else if(inputSize.val() > 15) {
                size = 15;
            }else {
                size = inputSize.val();
            }
            location.href = "myblog_list.html?page=" + page + "&size=" + size;
        }
    </script>
</body>

</html>